import { Application, IDesignViewOption, IAuditDesignViewOption, IOrderViewOption, } from '@manycore/custom-ksg-viewer-sdk';
import { useState } from 'react';
import { createContainer } from 'unstated-next';


export enum EViewType {
    /** 设计方案 */
    Design = 'design',
    /** 审核方案 */
    AuditDesign = 'auditDesign',
    /** 审核订单 */
    AuditOrder = 'auditOrder',
}

export type IViewInfo = { type: EViewType.Design, info: IDesignViewOption } | { type: EViewType.AuditDesign, info: IAuditDesignViewOption } | { type: EViewType.AuditOrder, info: IOrderViewOption };

export function useAppStore(initialState: {
    /** 模型展示app */
    app?: Application;
    /** 审核方案id */
    auditDesignId?: string;
    /** 展示数据 */
    viewInfo?: IViewInfo;
    /** 模型JSON数据 */
    jsonData?: any;
}) {
    const [appState, setAppState] = useState(initialState);

    const updateApplication = (app: Application) => {
        setAppState((prevState) => ({
            ...prevState,
            app
        }))
    }

    /**
     * 更新json数据
     * @param jsonData
     */
    const updateJsonData = (jsonData: any) => {
        setAppState((prevState) => ({
            ...prevState,
            jsonData
        }))
    }

    const updateViewInfo = (viewInfo: IViewInfo) => {
        setAppState((prevState) => ({
            ...prevState,
            viewInfo
        }))
    }


    return {
        appState,
        updateApplication,
        updateJsonData,
        updateViewInfo
    }
}


export const AppStore = createContainer(useAppStore);
